package com.changyinBus.system.service.impl;

import com.changyinBus.common.core.domain.TreeSelect;
import com.changyinBus.common.core.domain.entity.SysNavi;
import com.changyinBus.common.utils.StringUtils;
import com.changyinBus.system.mapper.SysNaviMapper;
import com.changyinBus.system.service.ISysNaviService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class SysNaviServiceImpl implements ISysNaviService {
    @Resource
    private SysNaviMapper sysNaviMapper;

    @Override
    public List<SysNavi> selectNaviList(SysNavi navi) {
        return sysNaviMapper.selectNaviList(navi);
    }

    @Override
    public SysNavi selectNaviById(Long naviId) {
        return sysNaviMapper.selectNaviById(naviId);
    }

    @Override
    public SysNavi selectNavByTypeAndTenantId(int navType,long tenantId) {
        return sysNaviMapper.selectNavByTypeAndTenantId(navType,tenantId);
    }

    @Override
    public List<SysNavi> selectChildrenNaviById(Long naviId) {
        return sysNaviMapper.selectChildrenNaviById(naviId);
    }

    @Override
    public int hasChildByNaviId(Long naviId) {
        return sysNaviMapper.hasChildByNaviId(naviId);
    }

    @Override
    public int insertNavi(SysNavi navi) {
        return sysNaviMapper.insertNavi(navi);
    }

    @Override
    public int updateNavi(SysNavi navi) {
        return sysNaviMapper.updateNavi(navi);
    }

    @Override
    public int updateNaviChildren(List<SysNavi> navis) {
        return sysNaviMapper.updateNaviChildren(navis);
    }

    @Override
    public int deleteNaviById(Long naviId) {
        return sysNaviMapper.deleteNaviById(naviId);
    }

    @Override
    public List<TreeSelect> selectNaviTreeList(SysNavi navi) {
        List<SysNavi> navis = selectNaviList(navi);
        return buildNaviTreeSelect(navis);
    }

    @Override
    public List<TreeSelect> buildNaviTreeSelect(List<SysNavi> navis) {
        List<SysNavi> nvaTrees = buildNaviTree(navis);
        return nvaTrees.stream().map(TreeSelect::new).collect(Collectors.toList());
    }

    @Override
    public List<SysNavi> buildNaviTree(List<SysNavi> navis) {
        List<SysNavi> returnList = new ArrayList();
        List<Long> tempList = navis.stream().map(SysNavi::getNavId).collect(Collectors.toList());
        for (SysNavi navi : navis) {
            // 如果是顶级节点, 遍历该父节点的所有子节点
            if (!tempList.contains(navi.getParentId())) {
                recursionFn(navis, navi);
                returnList.add(navi);
            }
        }
        if (returnList.isEmpty()) {
            returnList = navis;
        }
        return returnList;
    }

    /**
     * 递归列表
     */
    private void recursionFn(List<SysNavi> list, SysNavi t) {
        // 得到子节点列表
        List<SysNavi> childList = getChildList(list, t);
        t.setChildren(childList);
        for (SysNavi tChild : childList) {
            if (hasChild(list, tChild)) {
                recursionFn(list, tChild);
            }
        }
    }

    /**
     * 得到子节点列表
     */
    private List<SysNavi> getChildList(List<SysNavi> list, SysNavi t) {
        List<SysNavi> tlist = new ArrayList<SysNavi>();
        Iterator<SysNavi> it = list.iterator();
        while (it.hasNext()) {
            SysNavi n = (SysNavi) it.next();
            if (StringUtils.isNotNull(n.getParentId()) && n.getParentId() == t.getNavId()) {
                tlist.add(n);
            }
        }
        return tlist;
    }

    /**
     * 判断是否有子节点
     */
    private boolean hasChild(List<SysNavi> list, SysNavi t) {
        return getChildList(list, t).size() > 0;
    }
}
